import EventEmitterUtils from "./EventEmitterUtils";

export default {
    saveCartCountToLocal(productCount) {
        const shopCartCount = localStorage.getItem("shopCartCount");
        let oldCount = 0;
        if (shopCartCount) {
            oldCount = shopCartCount;
        }
        let count = oldCount - 0 + (productCount - 0);
        localStorage.setItem("shopCartCount", count);
    },

    saveCartInfoToLocal(productId, productCount) {
        const oldShopCartInfoStr = localStorage.getItem("shopCartInfo");
        if (oldShopCartInfoStr) {
            let oldShopCartInfo = JSON.parse(oldShopCartInfoStr);
            if (
                this.isProductIdInCart(oldShopCartInfo, productId, productCount)
            ) {
                localStorage.setItem(
                    "shopCartInfo",
                    JSON.stringify(oldShopCartInfo)
                );
            } else {
                oldShopCartInfo.push({ count: productCount, productId });
                localStorage.setItem(
                    "shopCartInfo",
                    JSON.stringify(oldShopCartInfo)
                );
            }
        } else {
            let shopCartInfo = [
                {
                    count: productCount,
                    productId
                }
            ];
            localStorage.setItem("shopCartInfo", JSON.stringify(shopCartInfo));
        }
    },

    isProductIdInCart(oldShopCartInfo, productId, productCount) {
        for (let i = 0; i < oldShopCartInfo.length; i++) {
            if (oldShopCartInfo[i].productId == productId) {
                oldShopCartInfo[i].count =
                    oldShopCartInfo[i].count - 0 + (productCount - 0);
                return true;
            }
        }
        return false;
    },

    updateCartInfo(productId, curentCount) {
        const oldShopCartInfoStr = localStorage.getItem("shopCartInfo");
        if (oldShopCartInfoStr) {
            let oldShopCartInfo = JSON.parse(oldShopCartInfoStr);
            for (let i = 0; i < oldShopCartInfo.length; i++) {
                if (oldShopCartInfo[i].productId == productId) {
                    oldShopCartInfo[i].count = curentCount;
                }
            }
            localStorage.setItem(
                "shopCartInfo",
                JSON.stringify(oldShopCartInfo)
            );
        }
    },

    deleteCartCount(productId) {
        const oldShopCartInfoStr = localStorage.getItem("shopCartInfo");
        let totalCount = localStorage.getItem("shopCartCount");
        if (oldShopCartInfoStr) {
            try {
                let oldShopCartInfo = JSON.parse(oldShopCartInfoStr);
                for (let i = 0; i < oldShopCartInfo.length; i++) {
                    if (oldShopCartInfo[i].productId == productId) {
                        let oldItemCount = oldShopCartInfo[i].count;
                        totalCount = totalCount - oldItemCount;
                    }
                }

                let newShopCartInfo = oldShopCartInfo.filter(item => {
                    return item.productId != productId;
                });
                localStorage.setItem(
                    "shopCartInfo",
                    JSON.stringify(newShopCartInfo)
                );
                localStorage.setItem("shopCartCount", totalCount);
            } catch (e) {
                console.log(e);
            }
        }
        EventEmitterUtils.emit("updateShopCartNum", totalCount);
    },

    deleteCartCountByArr(productIds) {
        const oldShopCartInfoStr = localStorage.getItem("shopCartInfo");
        let totalCount = 0;
        if (oldShopCartInfoStr) {
            let oldShopCartInfo = JSON.parse(oldShopCartInfoStr);
            let newShopCartInfo = [];
            for (let i = 0; i < oldShopCartInfo.length; i++) {
                if (!productIds.includes(oldShopCartInfo[i].productId)) {
                    let oldItemCount = oldShopCartInfo[i].count;
                    totalCount += oldItemCount;
                    newShopCartInfo.push(oldShopCartInfo[i]);
                }
            }
            localStorage.setItem(
                "shopCartInfo",
                JSON.stringify(newShopCartInfo)
            );
            localStorage.setItem("shopCartCount", totalCount);
        }
        EventEmitterUtils.emit("updateShopCartNum", totalCount);
    }
};
